
#ifndef __HEM_COMMON_H_
#define __HEM_COMMON_H_

#include "hem_port.h"
#include "driver/adc/drv_adc.h"
#include "driver/timer/drv_timer.h"
#include "driver/usart/drv_usart.h"
#include "driver/pwm/drv_pwm.h"
#include "gd32f4xx.h"
#include "gd32f4xx_gpio.h"
#include "gd32f4xx_rcu.h"
#include "gd32f4xx_timer.h"
#include "user_define.h"
#include "systick.h"
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "main.h"

#define PIN_CFG(PORT, PIN, MODE, OTYPE, PUPD, OSPPED, AF)                      \
  GPIO##PORT, GPIO_PIN_##PIN, GPIO_MODE_##MODE, GPIO_OTYPE_##OTYPE,            \
      GPIO_PUPD_##PUPD, GPIO_OSPEED_##OSPPED, GPIO_AF_##AF

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MAX3(a, b, c) MAX(MAX(a, b), c)
#define MIN3(a, b, c) MIN(MIN(a, b), c)

#define FLASH_CONFIG_OK     0xaa55
#define HEM_PWM_FREQ_NUM    6
#define AI_PWMI_MBER        ((HEM_PWMI_NUM + 3) / 4 + 1)
#define AI_COM_NUMBER       ((HEM_AI_NUM   + 1) / 4 + 1)

#define CYCLE_MAX_NUMBER 15
#define PWM_2000Hz 2000
#define PWM_1000Hz 1000
#define PWM_500Hz 500
#define PWM_250Hz 250
#define PWM_200Hz 200
#define PWM_125Hz 125
#define PWM_100Hz 100
#define PWM_80Hz 80
#define PWM_75Hz 75
#define PWM_50Hz 50
#define PWM_25Hz 25

#define SPI_FLASH_INDEX 0

#define ADC_RESOLUTION_BITS 12
#define ADC_MAX_VALUE ((1ul << (ADC_RESOLUTION_BITS)) - 1)
#define ADC_VREF_VOLTAGE 3.3f // V

enum PANEL_LED {
  xSPEED_LED, // xSPEED（LED指示灯）
  STATUS_LED, // STATUS（LED指示灯）
  FAULT_LED,  // FAULT（LED指示灯）
  PANEL_LED_NUM,
};

enum PANEL_LED_STATUS {
  LED_OFF,
  LED_ON,
};


typedef struct hem_pin_cfg_tag {
  uint32_t port;  /* GPIO port definitions */
  uint32_t pin;   /* GPIO pin definitions */
  uint32_t mode;  /* GPIO mode configuration values:GPIO_MODE_INPUT、
                  GPIO_MODE_OUTPUT、GPIO_MODE_AF、GPIO_MODE_ANALOG
                   */
  uint32_t otype; /* push pull mode/open drain mode */
  uint32_t pupd;  /* GPIO pull-up/pull-down values */
  uint32_t speed; /* GPIO output speed values */
  uint32_t af;    /* GPIO alternate function */
} hem_pin_cfg_t;

typedef struct hem_pin_info_tag {
  uint32_t port; /* GPIO port definitions */
  uint32_t pin;  /* GPIO pin definitions */
} hem_pin_info_t;

typedef struct hem_nvic_info_tag {
  uint8_t nvic_irq;
  uint8_t nvic_irq_pre_priority;
  uint8_t nvic_irq_sub_priority;
} hem_nvic_info_t;

typedef struct sensor_tag {
  uint16_t ad;
  uint16_t val;
} sensor_t;

typedef struct au_sen_calibrate_tag {
  uint16_t flag;
  sensor_t min;
  sensor_t max;
} au_sen_calibrate_t;


typedef struct CAN_Tx_FIFO
{
    uint32_t id; 
    uint8_t buf[8];
    uint8_t len;
    uint8_t ide; 
    uint8_t valid;
}CAN_Tx_FIFO_t;

void hem_rcu_init(void);
void hem_pin_init(void);

uint32_t check_error(void);

void delay_ms(uint32_t count);
/**
 * @brief 自动更新结构体gp_io参数中的输入信号
 * 如果使用结构体中的输入功能，该函数必须保留；
 * 建议将该函数放到1ms定时器中；
 */
void input_update(void);
/**
 * @brief 输出自动更新函数，根据结构体参数gp_io中的数据自动更新；
 * 如果使用结构体中的输出功能，该函数必须保留；
 * 如果使用PWMi的功能，该函数必须放到1ms定时器中；
 */
void output_update(void);


#endif
